﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PSSMS.View.AccPay;

namespace PSSMS.View.SysManage
{ 
    /// <summary>
    /// ProviderSM.xaml 的交互逻辑
    /// </summary>
    public partial class ProviderSM : UserControl
    {
        BLL.Providersm.ProvidersmClient myClient = new BLL.Providersm.ProvidersmClient();
        string strContent = string.Empty;//条件

        //实例化表格：接收筛选数据
        DataTable dtNew = new DataTable();
        public ProviderSM()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用户控件加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProviderSm_Loaded(object sender, RoutedEventArgs e)
        {
            //查询供货商信息
            ProviderSm(strContent);
            //控制修改和删除按钮禁用
            btnReset.IsEnabled = false;//禁用修改按钮
            btnReset.Opacity = 0.5;
            btnSave.IsEnabled = false;//禁用删除按钮
            btnSave.Opacity = 0.5;
        }

        /// <summary>
        /// 条件筛选,TxtSelect
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtSelect_SelectionChanged(object sender, RoutedEventArgs e)
        {
            //获取全部数据
            strContent = TxtSelect.Text.Trim();//获取控件值
            ProviderSm(strContent);//刷新数据
            //DataTable dtAll = myClient.CheckProvidersm().Tables[0];
            //try
            //{
            //    string strContent = "";//条件变量
            //    string strText = TxtSelect.Text.Trim();//获取控件文本值
            //    //判断控件值不为空
            //    if (strText != "")
            //    {
            //        //模糊查询SQL语句
            //        strContent += "supplierName like '%" + strText + "%" +
            //             " or contactPerson like '%" + strText + "%'" +
            //                  " or supplierType like '%" + strText + "%'" +
            //                  " or remark like '%" + strText + "%'";
            //    }
            //    DataView dv = new DataView(dtAll);//把数据转换成自定义视图

            //    //判断筛选条件是否为空
            //    if (strContent != "")
            //    {
            //        //条件筛选:设置表达式筛选数据
            //        dv.RowFilter = strContent;
            //        dtNew = dv.ToTable();//刷新数据
            //        //ProviderSm();//刷新数据
            //    }
            //    else
            //    {
            //        //查询全部数据
            //        dtNew = dtAll;
            //    }
            //}
            //catch (Exception)
            //{
            //    return;
            //}
        }

        /// <summary>
        /// 选中行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DgrdModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //判断是否选中数据
            if (DgrdModel.SelectedItem != null)
            {
                //操作按钮
                btnReset.IsEnabled = true;//启用修改按钮
                btnReset.Opacity = 1;
                btnSave.IsEnabled = true;//启用删除按钮
                btnSave.Opacity = 1;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="strContent"></param>
        private void ProviderSm(string strContent)
        {
            //调用服务查询方法
            dtNew = myClient.CheckProvidersm(strContent).Tables[0];
            DgrdModel.ItemsSource = dtNew.DefaultView;
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                strContent = TxtSelect.Text.Trim();
                //调用查；刷新数据
                ProviderSm(strContent);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 供货商增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void prAdd(object sender, RoutedEventArgs e)
        {
            ProviderAdd insert = new ProviderAdd();
            insert.ShowDialog();
            ProviderSm(strContent);//刷新数据
        }

        /// <summary>
        /// 打开修改窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取表格选中行
                    DataRowView data = (DataRowView)DgrdModel.SelectedItem;
                    //3、实例化修改窗口创建对象
                    Provider_xg padd = new Provider_xg(data);
                    padd.ShowDialog();//打开窗口
                    ProviderSm(strContent);//刷新数据
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //1、判断是否选中表格数据
                if (DgrdModel.SelectedItem != null)
                {
                    //2、获取选中行
                    DataRowView rowView = (DataRowView)DgrdModel.SelectedItem;
                    //3、提取单元格获取主键
                    int intsupplierID = Convert.ToInt32(rowView.Row["supplierID"]);
                    //4、调用方法执行删除
                    int intCount = myClient.DelectStaff(intsupplierID);
                    if (intCount > 0)
                    {
                        //5、提示
                        MessageBox.Show("删除成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                        //6、刷新
                        ProviderSm(strContent);//刷新数据
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void print_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断表格是否有数据
                if (DgrdModel.Items.Count > 0)
                {
                    DY_ghsPr Dy = new DY_ghsPr(dtNew);
                    Dy.ShowDialog();//打开窗口
                }
                else
                {
                    MessageBox.Show("表格无数据！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Stop);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 打开导入窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            DR_ghsPr mydaoru = new DR_ghsPr();
            mydaoru.ShowDialog();
            //刷新数据
            ProviderSm(strContent);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExcel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断表格是否有数据
                if (DgrdModel.Items.Count > 0)
                {
                    //调用导出方法
                    Tools.Utils.ExportToExcel.Export(DgrdModel, "供货商信息");
                }
                else
                {
                    MessageBox.Show("表格无数据！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Stop);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        
        /// <summary>
        /// 退出当前页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_tuichu_Click(object sender, RoutedEventArgs e)
        {
          
        }
    }
}
